;
(function() {
  var SERVERId = hlUrlUtil.GetQueryString('id'); //服务id

  new Vue({
    el: '#dd-config',
    data: {
      navLiData: [{
        name: '项目配置',
        code: true
      }, {
        name: '客户账号管理',
        code: true
      }],
      navAct: 0,
      permissions: JSON.parse(window.localStorage.getItem('permissions')),
      serviceInfo: '',
      serverId: SERVERId,
      userProFlag: false,
      proList: [],
      userProData: [],
      keyWord: '',
      customerList: [],
      ddUser: [],
      isBindalert: false,
      selectedcustomer: '',
      alluser: [],
    },
    filters: {
      timestamp: function(time) {
        if (!time) return '--';
        return hlDateUtil.formatUTCDate(time)
      },
      toDou: function(n) {
        return n < 10 ? '0' + n : '' + n;
      }
    },
    computed: {
      selectedList() {
        var list = []
        this.userProData.forEach(item => {
          if (item.selected == true) {
            list.push(item)
          }
        })
        return list
      }
    },
    methods: {
      // 导航切换
      navButton: function(i) {
        this.navAct = i;
        var is = this;
        if (i == 1) {
          //获取公众号项目列表
          this.getProject()
        }
        if (i == 2) {
          this.getFocusOnList()
        }
      },
      // 获取第三方服务信息
      getservice_info() {
        var that = this
        hlhttp.get({
          url: 'tenant/corp/service_info',
          data: {
            id: that.serverId
          },
          success: function(data) {
            that.serviceInfo = data
          },
        })
      },
      // 获取钉钉应用绑定项目列表
      getProject: function() {
        var that = this;
        hlhttp.get({
          url: 'tenant/dd_app/projects',
          data: {
            corp_service_id: that.serverId
          },
          success: function(res) {
            that.proList = res
          }
        })
      },
      //配置项目钉钉
      configPro: function() {
        var is = this;
        var ids = [];
        is.selectedList.forEach(item => {
          ids.push(item.projectId)
        })
        hlhttp.put({
          url: 'tenant/dd_app/bind_projects',
          data: {
            corp_service_id: is.serverId,
            project_ids: ids.toString() || '#'
          },
          success: function() {
            hl.toast('保存成功')
            is.checkAllFlag = false;
            is.userProFlag = false;
            is.putUserData = [];
            is.getProject()
          }
        })
      },

      //获取当前用户项目列表
      getUserPro: function() {
        var that = this;
        this.userProFlag = true;
        hlhttp.get({
          url: 'tenant/fn_projects',
          success: function(res) {
            res.forEach(item => {
              item.selected = false
            })
            that.proList.forEach(item => {
              res.some(single => {
                if (item.projectId == single.projectId) {
                  single.selected = true
                }
              })
            })
            that.userProData = res
          }
        })
      },

      // 单选
      checkRadio(item, index) {
        this.userProData[index].selected = !this.userProData[index].selected
      },

      //全选项目
      checkAllPro() {
        this.userProData.forEach(item => {
          item.selected = true
        })
      },
      //查询客户账号
      search() {
        this.customerData();
      },
      // 重置客户账号
      reset() {
        this.keyWord = ''
        this.customerData();
      },
      // 获取所有客户列表
      customerData() {
        var self = this;
        hlhttp.get({
          url: 'tenant/customers',
          data: {
            keyword: this.keyWord || '#'
          },
          success: function(data) {
            self.customerList = data
          }
        })
      },

      //获取客户下绑定的钉钉用户
      getdd_users(id) {
        var that = this
        hlhttp.get({
          url: 'customer/dd_users',
          data: {
            customer_id: id,
            corp_service_id: SERVERId
          },
          success: function(data) {
            that.alluser = []
            data.forEach(item => {
              item.account = item.userAccount
              item.id = item.userId
              item.name = item.userName
              item.phone = item.userPhone
              that.alluser.push(item)
            })
            that.ddUser = data
          }
        })
      },



      //绑定客户账号弹框
      bindaccount() {
        if (!this.selectedcustomer) {
          hl.toast('请选中要客户')
          return
        }
        this.isBindalert = true
        setTimeout(() => {
          approve.data.getCount = false
          approve.data.memberType = 'A';
          approve.data.ddflag = true
          approve.data.selectPerson = this.alluser
          approve.dom.addinitialDom()
          approve.methods.ddwxTab();
          approve.event.wxddNodeEvent((data) => {
            this.alluser = data
          })

        }, 30)
      },
      // 减少客户（弹出框右侧）
      reduceCus(index) {
        this.alluser.splice(index, 1)
        approve.data.selectPerson = this.alluser
        approve.dom.showRight()
      },
      // 选择客户
      selectedcus(item, index) {
        this.selectedcustomer = item.customerId
        this.getdd_users(item.customerId)
      },
      //确认 (绑定钉钉用户到客户)
      surebind() {
        var arr = []
        this.alluser.forEach(item => {
          arr.push(item.id)
        })
        var that = this
        hlhttp.put({
          url: 'customer/bind_dd_users',
          data: {
            customer_id: that.selectedcustomer,
            corp_service_id: SERVERId,
            user_ids: arr.length ? arr.join(',') : '#'
          },
          success: function(data) {
            that.isBindalert = false
            that.getdd_users(that.selectedcustomer)
          }
        })
      },


    },
    mounted: function() {
      this.getservice_info();
      this.getProject();
      this.customerData();
    }
  })
}())
